﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using ICeFramework.Common.DataLayer.Enums;
using ICeFramework.Common.Extenstions;

namespace ICeFramework.Common.Configuration
{
    public class ConfigurationHelper
    {
        #region public static string GetConnection()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetConnection()
        {
            return GetConnection(string.Empty);
        }
        #endregion

        #region public static string GetConnection(string name)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetConnection(string name)
        {
            DataAccessSectionHandler das = (DataAccessSectionHandler)ConfigurationManager.GetSection(DataAccessSectionHandler.SectionName);
            if (das == null)
            {
                throw new NullReferenceException("Cannot find default connection string");
            }

            if (string.IsNullOrEmpty(name))
            {
                return GetConnectionString(das.DefaultConnection.Name);
            }

            foreach (ConnectionElement elem in das.Connections)
            {
                if (elem.Name == name)
                {
                    return GetConnectionString(elem.Name);
                }
            }
            return GetConnectionString(das.DefaultConnection.Name);
        }
        #endregion

        #region public static string GetRemoteUrl(string name)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetRemoteUrl(string connectionName)
        {
            DataAccessSectionHandler das = (DataAccessSectionHandler)ConfigurationManager.GetSection(DataAccessSectionHandler.SectionName);
            if (das == null)
            {
                throw new NullReferenceException("Cannot find default connection");
            }

            if (string.IsNullOrEmpty(connectionName))
            {
                return das.DefaultConnection.RemoteUrl;
            }
            foreach (ConnectionElement elem in das.Connections)
            {
                if (elem.Name == connectionName)
                {
                    return elem.RemoteUrl;
                }
            }
            return das.DefaultConnection.RemoteUrl;
        }
        #endregion

        #region public static string GetRemoteUserName(string connectionName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        public static string GetRemoteUserName(string connectionName)
        {
            DataAccessSectionHandler das = (DataAccessSectionHandler)ConfigurationManager.GetSection(DataAccessSectionHandler.SectionName);
            if (das == null)
            {
                throw new NullReferenceException("Cannot find default connection");
            }

            if (string.IsNullOrEmpty(connectionName))
            {
                return das.DefaultConnection.RemoteUserName;
            }
            foreach (ConnectionElement elem in das.Connections)
            {
                if (elem.Name == connectionName)
                {
                    return elem.RemoteUserName;
                }
            }
            return das.DefaultConnection.RemoteUserName;
        }
        #endregion

        #region public static string GetRemotePassword(string connectionName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        public static string GetRemotePassword(string connectionName)
        {
            DataAccessSectionHandler das = (DataAccessSectionHandler)ConfigurationManager.GetSection(DataAccessSectionHandler.SectionName);
            if (das == null)
            {
                throw new NullReferenceException("Cannot find default connection");
            }

            if (string.IsNullOrEmpty(connectionName))
            {
                return das.DefaultConnection.RemotePassword;
            }
            foreach (ConnectionElement elem in das.Connections)
            {
                if (elem.Name == connectionName)
                {
                    return elem.RemotePassword;
                }
            }
            return das.DefaultConnection.RemotePassword;
        }
        #endregion

        #region public static ServerType GetServerType()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ServerType GetServerType()
        {
            return GetServerType(string.Empty);
        }
        #endregion

        #region public static ServerType GetServerType(string connectionName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        public static ServerType GetServerType(string connectionName)
        {
            DataAccessSectionHandler das = (DataAccessSectionHandler)ConfigurationManager.GetSection(DataAccessSectionHandler.SectionName);
            if (das == null)
            {
                throw new NullReferenceException("Cannot find default connection string");
            }

            if (string.IsNullOrEmpty(connectionName))
            {
                return das.DefaultConnection.ServerType;
            }
            foreach (ConnectionElement elem in das.Connections)
            {
                if (elem.Name == connectionName)
                {
                    return elem.ServerType;
                }
            }
            return das.DefaultConnection.ServerType;
        }
        #endregion

        #region private string GetConnectionString(string name)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static string GetConnectionString(string name)
        {
            foreach (ConnectionStringSettings cs in ConfigurationManager.ConnectionStrings)
            {
                if (cs.Name == name)
                {
                    return cs.ConnectionString;
                }
            }
            return null;
        }
        #endregion

        #region public static string GetCustomConfigValue(string sectionName, string keyName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static string GetCustomConfigValue(string sectionName, string keyName)
        {
            NameValueCollection myData = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection(sectionName);

            if (myData == null)
            {
                throw (new NullReferenceException("Section Name not found: " + sectionName));
            }

            string myValue = myData.Get(keyName);
            if (myValue == null)
            {
                throw (new NullReferenceException("Key Name not found: " + keyName));
            }
            return myValue;
        }
        #endregion

        #region public static T GetValue<T>(string key)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetValue<T>(string key)
        {
            string val = ConfigurationManager.AppSettings[key];
            return val.Parse<T>();
        }
        #endregion
    }
}
